package com.example.module_motorcircle.detail.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.lib_net.model.api.AfterSalePageItem
import com.example.lib_net.model.api.PostCommentItem
import com.example.lib_net.model.api.PostItem
import com.example.module_motorcircle.databinding.ItemAfterSaleInfoBinding
import com.example.module_motorcircle.databinding.Layout1stClassCommentsItemBinding
import com.example.module_motorcircle.databinding.Layout2ndClassCommentsItemBinding
import com.example.module_motorcircle.databinding.LayoutCommentsFooterBinding
import com.example.module_motorcircle.databinding.LayoutCommentsHeaderBinding
import com.example.module_motorcircle.databinding.LayoutPostDetailsItemBinding

/**
 * @author yaotianxue
 * @description  多布局展示数据
 * @date 2025/2/14
 */
class PostDetailAdapter(
    private val onClickComments:((comment:PostCommentItem,isFirst:Boolean)->Unit)? = null,
):RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private var haveAfterSaleInfo:Boolean = false //是否有售后信息
    private var totalCommentsSize:Int = 0 //评论个数
    private var mCommentOffsetInData:Int = 2 + if(haveAfterSaleInfo) 1 else 0//评论偏移量  2是帖子详情和头布局 + 售后信息


    //数据
    var currentPost: PostItem? = null //详情数据
    var afterSaleList:List<AfterSalePageItem>? = null //售后信息数据
    var commentist:MutableList<PostCommentItem> = mutableListOf() //评论信息数据



    /**
     * 设置帖子详情
     */
    fun setPostInfo(post:PostItem){
        this.currentPost = post
        //局部刷新
        notifyDataSetChanged()
    }

    /**
     * 设置售后信息
     */
    fun setAfterSaleInfo(list:List<AfterSalePageItem>){
        this.haveAfterSaleInfo = !list.isNullOrEmpty() //判断是否有售后信息
        this.afterSaleList = list
        //局部刷新
        notifyDataSetChanged()
    }
    /**
     * 设置评论信息
     */
    fun setCommentInfo(list:MutableList<PostCommentItem>,totalSize:Int){
        this.totalCommentsSize = totalSize
        commentist.clear()
        commentist.addAll(list)
        //局部刷新
        notifyDataSetChanged()
    }
    /**
     * 添加评论信息
     */
    fun addCommentInfo(list:MutableList<PostCommentItem>){
        commentist.addAll(list)
    }
    override fun getItemCount(): Int {
        //评论内容+底部+评论头+帖子详情+售后信息
        return commentist.size + 1 + 2 + if(haveAfterSaleInfo) 1 else 0
    }

    /**
     * 多布局类型
     */
    override fun getItemViewType(position: Int): Int {
        if(haveAfterSaleInfo){//有售后信息
            return when(position){
                0 -> Type.AFTER_SALE_INFO.value //售后信息
                1 -> Type.POST.value //帖子详情
                2 -> Type.COMMENTS_HEADER.value //评论头 Xxx条评论
                in 3 .. commentist.size + 2 -> //评论内容
                    return if(commentist[position - mCommentOffsetInData].isFirstLevel) Type.FIRST_COMMENTS.value else Type.SECOND_COMMENTS.value
                else -> Type.COMMENTS_FOOTER.value //评论底部
            }
        }else{//没有售后信息
            return when(position){
                0 -> Type.POST.value
                1 -> Type.COMMENTS_HEADER.value
                in 2 .. commentist.size + 1 ->
                    return if(commentist[position - mCommentOffsetInData].isFirstLevel) Type.FIRST_COMMENTS.value else Type.SECOND_COMMENTS.value
                else -> Type.COMMENTS_FOOTER.value
            }
        }

    }

    /**
     * 根据不同类型创建不同的viewholder
     */
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        when(viewType){
            Type.AFTER_SALE_INFO.value -> {
                val binding = ItemAfterSaleInfoBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )

                return AfterSaleViewHolder(binding)
            }
            Type.POST.value -> {
                val binding = LayoutPostDetailsItemBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )

                return PostDetailViewHolder(binding)
            }
            Type.COMMENTS_HEADER.value -> {
                val binding = LayoutCommentsHeaderBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
                return CommentsHeaderViewHolder(binding)
            }
            Type.FIRST_COMMENTS.value -> {
                val binding = Layout1stClassCommentsItemBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
                return FirstClassCommentsViewHolder(binding)
            }
            Type.SECOND_COMMENTS.value -> {
                val binding = Layout2ndClassCommentsItemBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
                return SecondClassCommentsViewHolder(binding)
            }
            else -> {
                val binding = LayoutCommentsFooterBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
                return CommentsFooterViewHolder(binding)
            }
        }
    }


    /**
     * 根据不同类型绑定数据
     */
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when(getItemViewType(position)){
            Type.AFTER_SALE_INFO.value -> { //售后信息


            }
            Type.POST.value -> {//帖子详情
                val viewHolder = holder as PostDetailViewHolder
                viewHolder.binding.post = currentPost
                viewHolder.binding.holder = viewHolder

                //复杂数据处理:九宫格话题处理
                currentPost?.let {
                    viewHolder.initView(it,position)
                }

            }
            Type.COMMENTS_HEADER.value -> { //评论头信息
                val viewHolder = holder as CommentsHeaderViewHolder
                viewHolder.binding.tvCommentsNum.text = String.format("%s条评论",totalCommentsSize)
            }
            Type.FIRST_COMMENTS.value -> {//一级评论
                val viewHolder = holder as FirstClassCommentsViewHolder
                viewHolder.binding.comments = commentist[position - mCommentOffsetInData]
                viewHolder.binding.viewHolder = viewHolder
                //点击一级评论
                viewHolder.binding.root.setOnClickListener {
                    onClickComments?.invoke(commentist[position - mCommentOffsetInData],true)
                }
            }
            Type.SECOND_COMMENTS.value -> {//二级评论
                val viewHolder = holder as SecondClassCommentsViewHolder
                viewHolder.binding.comments = commentist[position - mCommentOffsetInData]
                viewHolder.binding.viewHolder = viewHolder
                //点击二级评论
                viewHolder.binding.root.setOnClickListener {
                    onClickComments?.invoke(commentist[position - mCommentOffsetInData],false)
                }
            }
            Type.COMMENTS_FOOTER.value -> {//评论底部
                val viewHolder = holder as CommentsFooterViewHolder
                viewHolder.binding.viewHolder = viewHolder
                //状态切换
            }
        }

    }
}

enum class Type(val value: Int) {
    POST(1), COMMENTS_HEADER(2), FIRST_COMMENTS(3), SECOND_COMMENTS(4), COMMENTS_FOOTER(5), AFTER_SALE_INFO(6)
}